using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Deepcode.Flux.Core.Data;

namespace Deepcode.Flux.Core.Systems.CMS.Mappings
{
	/// <summary>
	/// Manager class for content domain mapping table
	/// </summary>
	public class DomainMappings : Deepcode.Flux.Core.Data.BaseManager
	{
		#region Constructors
		public DomainMappings() : base() { }
		public DomainMappings(SqlConnection CurrentConnection) : base(CurrentConnection) { }
		public DomainMappings(SqlConnection CurrentConnection, SqlTransaction CurrentTransaction) : base(CurrentConnection, CurrentTransaction) { }
		#endregion
		#region Data Retrieval Methods
		/// <summary>
		/// Gets all of the known mappings on the system
		/// </summary>
		/// <returns></returns>
		public DomainMapping [] GetAll()
		{
			List<DomainMapping> results = new List<DomainMapping>();
			DataTable result = new DataTable();

			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("coreCMSMapGetAll");
			da.Fill(result);
			ReleaseConnection();

			foreach (DataRow r in result.Rows)
				results.Add(new DomainMapping(r));

			return results.ToArray();
		}
		/// <summary>
		/// Gets the specific mapping by it's ID
		/// </summary>
		/// <param name="ID"></param>
		/// <returns></returns>
		public DomainMapping GetByID(int ID)
		{
			DataTable result = new DataTable();
			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("coreCMSMapGetByID");
			da.SelectCommand.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, ID));
			da.Fill(result);
			ReleaseConnection();

			if (result == null) return null;
			if (result.Rows.Count < 1) return null;

			return new DomainMapping(result.Rows[0]);
		}
		/// <summary>
		/// Gets a specific mapping by domain name
		/// </summary>
		/// <param name="Domain"></param>
		/// <returns></returns>
		public DomainMapping GetByDomain(string Domain)
		{
			DataTable result = new DataTable();
			EstablishConnection();
			SqlDataAdapter da = this.CreateDataAdapter("coreCMSMapGetByDomain");
			da.SelectCommand.Parameters.Add( SQLUtility.ParameterIn("@Domain", SqlDbType.NVarChar, 500, Domain));
			da.Fill( result );
			ReleaseConnection();

			if( result == null ) return null;
			if( result.Rows.Count < 1 ) return null;

			return new DomainMapping(result.Rows[0]);
		}
		#endregion
		#region Record Save/Delete
		/// <summary>
		/// Saves the mapping record to the database
		/// </summary>
		/// <param name="group"></param>
		/// <returns></returns>
		public int Save(DomainMapping map)
		{
			EstablishConnection();
			SqlCommand cmd = this.CreateSqlCommand("coreCMSMapSave");
			SqlParameter result = SQLUtility.ParameterInOut("@id", SqlDbType.Int, 4, map.PK_ID);
			cmd.Parameters.Add(result);
			cmd.Parameters.Add(SQLUtility.ParameterIn("@Domain", SqlDbType.NVarChar, 500, map.Domain));
			cmd.Parameters.Add(SQLUtility.ParameterIn("@FK_Homepage", SqlDbType.Int, 4, map.FK_Homepage));
			cmd.Parameters.Add(SQLUtility.ParameterIn("@FK_Rootpage", SqlDbType.Int, 4, map.FK_Rootpage));
			cmd.ExecuteNonQuery();
			ReleaseConnection();

			if (map.PK_ID == -1)
				map.PK_ID = (int)result.Value;

			return map.PK_ID;
		}
		/// <summary>
		/// Deletes the specified mapping from the database
		/// </summary>
		/// <param name="GroupID"></param>
		public void DeleteByID(int ID)
		{
			EstablishConnection();		
			SqlCommand cmd = this.CreateSqlCommand("coreCMSMapDelete");
			cmd.Parameters.Add(SQLUtility.ParameterIn("@id", SqlDbType.Int, 4, ID));
			cmd.ExecuteNonQuery();
			ReleaseConnection();
		}
		#endregion
	}
}
